
import { botByApp, botByPlatform, botByType, rank } from './util'

export async function getRank(type: string, plugins: string[], platform: string[], start = 0, end = Date.now()) {
	const data: Record<string, Record<string, any>> = {}
	const p: Promise<number>[] = []
	switch (type) {
		case 'bot':
			await botByType((bot_type, count) => {
				data[bot_type] = { sum: count }
				p.push(botByPlatform(bot_type, plugins, platform, start, end,
					(platform, count) => {
						data[bot_type][platform] = count
					}))
			})
			break
		case 'count':
			await botByType((bot_type, sum) => {
				p.push(botByApp(bot_type, plugins, platform, start, end,
					(app, plugin, count) => {
						data[bot_type] ??= { sum }
						data[bot_type][plugin] ??= {}
						data[bot_type][plugin][app] = count
					}))
			})
			break
		case 'plugin':
			return rank('plugin', start, end)
		case 'user':
			return rank('user_id', start, end)
		default:
			throw new Error('Invalid type')
	}
	await Promise.all(p)
	return data
}
